<?php
namespace Admin\Controller;
use app\mobile\controller\Base;
use Think\Controller;

/**
 * 会员信息控制器
 * Class MemberController
 * @package Admin\Controller
 */
class MemberController extends Controller{
    /**
     * 新增会员
     */
    public function addMember(){
        if(IS_AJAX){

            $arr_user['user'] = I('name');
            $arr_user['password'] = md5('1234567');
            $sex = I('post.sex');

            if($sex){
                $arr_user['sex'] = 1;// 男
            }else{
                $arr_user['sex'] = 2; // 女
            }

            $add_user = M('user')->add($arr_user);

            $pic = I('post.picture');
            $res_pic = M('picture')->where('id = "'.$pic.'"')->field('path')->find();
            $arr_pic['pic'] = $res_pic['path'];
            $arr_pic['userId'] = $add_user;

            $arr_detail['userId'] = $add_user;
            $arr_detail['useredId'] = 0;
            $arr_detail['phone'] = I('post.phone');
            $arr_detail['birthday'] = I('post.birthday');
            $arr_detail['addres'] = I('post.address_now');
            $arr_detail['regtime'] = time();
            $arr_detail['loadtime'] = time();

            $add_pic_res = M('pic1')->add($arr_pic);
            $add_detail_res = M('detail1')->add($arr_detail);

            $save_user['picId'] = $add_pic_res;
            $save_user['detailId'] = $add_detail_res;

            $res = M('user')->where("id = '".$add_user."'")->save($save_user);

            if($res){
                $this->ajaxReturn(['info'=>'新增成功','status'=>1,'url'=>U('Member/memberList')]);
            }else{
                $this->ajaxReturn(['info'=>'新增失败','status'=>0,'url'=>U('Member/addMember')]);
            }
        }else{
            $this->display();
        }
    }

    /**
     * 会员列表
     */
    public function memberList(){
        //实例化Memebr
        $user = M('user');

        // 导入分页类
        import('Org.Util.Page');

        //查询满足满足条件的总记录条数
        $count = $user->count();

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,3);

        //分页显示输出
        $show = $Page->show();

        $limit = $Page->firstRow . ',' . $Page->listRows;

        $data = M('user')->limit($limit)->order('id desc')->select();
        foreach($data as $k=>$v){
            if($v['level'] == 1){
                $ids1[] = $v['detailid'];
                $pic1[] = $v['picid'];
            }else if($v['level'] == 2){
                $ids2[] = $v['detailid'];
                $pic2[] = $v['picid'];
            }else if($v['level'] == 3){
                $ids3[] = $v['detailid'];
                $pic3[] = $v['picid'];
            }else if($v['level'] == 4){

                $ids4[] = $v['detailid'];
                $pic4[] = $v['picid'];
            }

        }

        $ids1 = implode(',',$ids1);
        $ids2 = implode(',',$ids2);
        $ids3 = implode(',',$ids3);
        $ids4 = implode(',',$ids4);
//        $pic1 = implode(',',$pic1);
//        $pic2 = implode(',',$pic2);
//        $pic3 = implode(',',$pic3);
//        $pic4 = implode(',',$pic4);
//
//        var_dump($ids1);
//        var_dump($ids3);die;

        if($ids1){
            $res_detail1 = M('detail1')->where('id in ('.$ids1.')')->select();
        }
        if($ids2){
            $res_detail2 = M('detail2')->where('id in ('.$ids2.')')->select();
        }
        if($ids3){
            $res_detail3 = M('detail3')->where('id in ('.$ids3.')')->select();
        }
        if($ids4){
            $res_detail4 = M('detail4')->where('id in ('.$ids4.')')->select();
        }
//        $res_detail1 = M('detail1')->where('id in ('.$ids1.')')->select();
//        $res_detail2 = M('detail2')->where('id in ('.$ids2.')')->select();
//        $res_detail3 = M('detail3')->where('id in ('.$ids3.')')->select();
//        $res_detail4 = M('detail4')->where('id in ('.$ids4.')')->select();
//        var_dump($res_detail2);die;
//        $res_pic1 = M('detail1')->where('id in ('.$pic1.')');
//        $res_pic2= M('detail1')->where('id in ('.$pic2.')');
//        $res_pic3 = M('detail1')->where('id in ('.$pic3.')');
//        $res_pic4 = M('detail1')->where('id in ('.$pic4.')');

        foreach($data as $k=>$v){
            foreach($res_detail1 as $k1=>$v1){

                if($v['id'] == $v1['userid']){
                      $data[$k]['phone'] = $v1['phone'];
                      $data[$k]['regtime'] = $v1['regtime'];
                }
            }

            foreach($res_detail2 as $k2=>$v2){
                if($v['id'] == $v2['userid']){
                    $data[$k]['phone'] = $v2['phone'];
                    $data[$k]['regtime'] = $v2['regtime'];
                }
            }

            foreach($res_detail3 as $k3=>$v3){

                if($v['id'] == $v3['userid']){
                    $data[$k]['phone'] = $v3['phone'];
                    $data[$k]['regtime'] = $v3['regtime'];
                }
            }

            foreach($res_detail4 as $k4=>$v4){

                if($v['id'] == $v4['userid']){
                    $data[$k]['phone'] = $v4['phone'];
                    $data[$k]['regtime'] = $v4['regtime'];
                }
            }

//            var_dump($res_detail2);die;

//            foreach($res_pic1 as $k5=>$v5){
//
//                if($v['id'] == $v5['userid']){
//
//                }
//            }

//            foreach($res_pic2 as $k6=>$v6){
//
//                if($v['id'] == $v6['userid']){
//
//                }
//            }

//            foreach($res_pic3 as $k7=>$v7){
//
//                if($v['id'] == $v7['userid']){
//
//                }
//            }

//            foreach($res_pic4 as $k8=>$v8){
//
//                if($v['id'] == $v8['userid']){
//
//                }
//            }
        }

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($data as $k=>$v){

            $data[$k]['level'] = $getlevel[$v['level']];
            $data[$k]['time'] = date('Y-m-d');
        }

        $this->assign('memberlist',$data);
        $this->assign('page',$show);
        $this->display();
    }

    /**
     * 编辑会员信息
     */
    public function editMember(){

        if(IS_AJAX){

            $arr_user['user'] = I('name');
            $sex = I('post.sex');
            if($sex == 1){
                $arr_user['sex'] = 1;// 男
            }else if($sex == 2){
                $arr_user['sex'] = 2; // 女
            }

            $id = I('member_id');
            $res_user = M('user')->where('id = "'.$id.'"')->save($arr_user);

            $res_user1 = M('user')->where('id = "'.$id.'"')->find();

            $pic = I('post.picture');
            $res_pic = M('picture')->where('id = "'.$pic.'"')->field('path')->find();
            $arr_pic['pic'] = $res_pic['path'];

            $user_picId = $res_user1['picid'];
            $user_detailId = $res_user1['detailid'];

            $arr_detail['phone'] = I('post.phone');
            $arr_detail['birthday'] = I('post.birthday');
            $arr_detail['addres'] = I('post.address_now');

            // 修改detail表和pic表
            $table1 = 'pic'.$res_user1['level'];
            $table2 = 'detail'.$res_user1['level'];

            $res_pic = M($table1)->where("id = '".$user_picId."'")->save($arr_pic);
            $res_detail = M($table2)->where("id = '".$user_detailId."'")->save($arr_detail);

            if($res_detail|| $res_pic || $res_user){
                $this->ajaxReturn(['info'=>'编辑成功','status'=>1,'url'=>U('Member/memberList')]);
            }else{
                $this->ajaxReturn(['info'=>'编辑失败','status'=>0,'url'=>U('Member/editMember',['id'=>$id])]);
            }
        }else{

            if($_GET['id']){

                $where['id'] = I('get.id');
            }else{
                $where['id'] = I('id');
            }
            $user = M('user')->where($where)->find();
//            var_dump($where);die;
            $table1 = 'detail'.$user['level'];
            $table2 = 'pic'.$user['level'];
            $res1 = M($table1)->where('id = "'.$user['detailid'].'"')->find();
            $res2 = M($table2)->where('id = "'.$user['picid'].'"')->find();

            $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

            $user['pic'] = C('domain').'/'.$res2['pic'];
            $user['phone'] = $res1['phone'];
            $user['level'] = $getlevel[$res1['level']];
            $user['regtime'] = date('Y-m-d',$res1['regtime']);
            $user['loadtime'] = date('Y-m-d',$res1['loadtime']);
            $user['addres'] = $res1['addres'];
            $user['birthday'] = $res1['birthday'];

            $this->assign('member',$user);
            $this->display();
        }
    }

    /**
     * 会员信息详情
     */
    public function detailMember(){
//        $member = M('Member')->where(['id'=>I('get.id')])->find();
        $user = M('user')->where('id = "'.I('get.id').'"')->find();
        $table1 = 'detail'.$user['level'];
        $table2 = 'pic'.$user['level'];

        $res1 = M($table1)->where('id = "'.$user['detailid'].'"')->find();
        $res2 = M($table2)->where('id = "'.$user['picid'].'"')->find();

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        $user['pic'] = C('domain').'/'.$res2['pic'];
        $user['phone'] = $res1['phone'];
        $user['level'] = $getlevel[$res1['level']];
        $user['regtime'] = date('Y-m-d',$res1['regtime']);
        $user['loadtime'] = date('Y-m-d',$res1['loadtime']);
        $user['addres'] = $res1['addres'];
        $user['birthday'] = $res1['birthday'];
        $this->assign('member',$user);
        $this->display();
    }
}